//
//  ZZPhotosViewController.m
//  ZeroCorner
//
//  Created by lanou3g on 15/11/19.
//  Copyright © 2015年 Zero_Zhou. All rights reserved.
//

#import "ZZPhotosViewController.h"
#import "UIImageView+WebCache.h"
#import "MJExtension.h"
#import "ZZHTTPManager.h"
#import "UIView+Frame.h"
#import "ZZphotoDetail.h"
#import "ZZphotoSet.h"




@interface ZZPhotosViewController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *countLabel;

@property (weak, nonatomic) IBOutlet UITextView *contentText;
@property (weak, nonatomic) IBOutlet UIScrollView *PhotoScrollview;


@property(nonatomic,strong) ZZphotoSet *photoSet; // </
@property(nonatomic,strong) NSArray *news; // </


@end

@implementation ZZPhotosViewController

-(NSArray *)news{
    if (_news == nil) {
        _news = [NSArray array];
        _news = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"NewsURLs.plist" ofType:nil]];
    }
    return _news;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
   
     self.navigationController.navigationBar.backgroundColor = [UIColor yellowColor];
    
    //取出关键字
    NSString *one = self.newsModel.photosetID;
    NSString *two = [one substringFromIndex:4];
    NSArray *three = [two componentsSeparatedByString:@"|"];
    

    NSString *url = [NSString stringWithFormat:@"http://c.m.163.com/photo/api/set/%@/%@.json",[three firstObject],[three lastObject]];
    // 发请求
    [self sendRequestWithUrl:url];
    
   }
-(void)viewWillAppear:(BOOL)animated{
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    self.tabBarController.tabBar.hidden = YES;
}

-(void)viewWillDisappear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = NO;
}
#pragma mark - 发请求
- (void)sendRequestWithUrl:(NSString*)url{
    [[ZZHTTPManager manager]GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        ZZphotoSet *photoSet = [ZZphotoSet objectWithKeyValues:responseObject];
        self.photoSet = photoSet;
        [self setLabelWithModel:photoSet];
        [self setImageViewWithModel:photoSet];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"failure%@",error);
    }];
}

/**
 * 设置页面文字
 */
- (void)setLabelWithModel:(ZZphotoSet*)photoSet{
    self.titleLabel.text = photoSet.setname;
    //设置新闻内容
    [self setContentWithIndex:0];
    NSString *countNum = [NSString stringWithFormat:@"1/%ld",photoSet.photos.count];
    self.countLabel.text = countNum;
}
/**
 * 设置页面imageview
 */
- (void)setImageViewWithModel:(ZZphotoSet*)photoSet{
    NSUInteger count = self.photoSet.photos.count;
    for (int i = 0; i < count; i++) {
        UIImageView *photoImgView = [[UIImageView alloc] init];
        photoImgView.height = self.PhotoScrollview.height;
        photoImgView.width = self.PhotoScrollview.width;
        photoImgView.y = -64;
        photoImgView.x = i * photoImgView.width;
        //图片的显示格式为合适大小
        photoImgView.contentMode = UIViewContentModeCenter;
        photoImgView.contentMode = UIViewContentModeScaleAspectFit;
        [self.PhotoScrollview addSubview:photoImgView];
    }
    [self setImgWithIndex:0];
    self.PhotoScrollview.contentOffset = CGPointZero;
    self.PhotoScrollview.contentSize = CGSizeMake(self.PhotoScrollview.width * count, 0);
    self.PhotoScrollview.showsHorizontalScrollIndicator = NO;
    self.PhotoScrollview.showsVerticalScrollIndicator = NO;
    self.PhotoScrollview.pagingEnabled = YES;
}

/**
 * 滚动完毕时调用
 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    int index = self.PhotoScrollview.contentOffset.x / self.PhotoScrollview.width;
    //添加图片
    [self setImgWithIndex:index];
    //添加文字
    NSString *countNum = [NSString stringWithFormat:@"%d/%ld",index+1,self.photoSet.photos.count];
    self.countLabel.text = countNum;
    //添加内容
    [self setContentWithIndex:index];
}
/**
 * 添加内容
 */
- (void)setContentWithIndex:(int)index{
    NSString *content = self.photoSet.photos[index][@"note"];
    NSString *contentTitle = self.photoSet.photos[index][@"imgtitle"];
    if (content.length != 0) {
        self.contentText.text = content;
    }else{
        self.contentText.text = contentTitle;
    }
}
/** 懒加载添加图片！这里才是设置图片 */
- (void)setImgWithIndex:(int)i
{
    
    UIImageView *photoImgView = nil;
    photoImgView.center = self.view.center;
    if (i == 0) {
        photoImgView = self.PhotoScrollview.subviews[i+2];
    }else{
        photoImgView = self.PhotoScrollview.subviews[i];
    }
    
    NSURL *purl = [NSURL URLWithString:self.photoSet.photos[i][@"imgurl"]];
    
    // 如果这个相框里还没有照片才添加
    if (photoImgView.image == nil) {
        [photoImgView sd_setImageWithURL:purl placeholderImage:[UIImage imageNamed:@"photoview_image_default_white"]];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)BackBtn:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (IBAction)shareAction:(id)sender {
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
